package com.zhouchao.springboot.server;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.springframework.stereotype.Component;

/**
 * @Author: zhouchao
 * @Date: 2021/06/10 11:12
 * @Description:
 */
@Component
public class TomcatWebServer implements  WebServer {

    private final Tomcat tomcat = new Tomcat();

    private final Object monitor = new Object();

    private volatile boolean started;

    @Override
    public void start() throws Exception {
        if (started){
            return;
        }
        synchronized (monitor){
            if (started) {
                return;
            }
            started = false;
            Connector connector = new Connector();
            //设置绑定端口
            connector.setPort(8080);
            tomcat.getService().addConnector(connector);
            tomcat.setConnector(connector);
            tomcat.getHost().setAutoDeploy(false);
            //创建应用上下文
            tomcat.addWebapp("", "E://");
            tomcat.start();
            tomcat.getServer().await();
        }
    }
}
